package service

import (
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/define"
	helper2 "MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"MyBlogv2/blog-common/oss"
	"MyBlogv2/blog-common/response"
	"github.com/gin-gonic/gin"
	"path"
)

func GetWebsiteInfo(c *gin.Context) {
	var websiteInfoVo models.WebsiteInfoVo
	var lastUpdateTime models.MyTime
	var articleCount int64
	var wordCount int
	var visitCount int
	var categoryCount int64
	var commentCount int64
	var allArticleContent []string
	models.Db.Model(new(models.WebsiteInfo)).First(&websiteInfoVo)
	models.Db.Model(new(models.Article)).Select("updated_at").Order("updated_at desc").First(&lastUpdateTime)
	models.Db.Model(new(models.Article)).Where("status = ?", define.PUBLIC_ARTICLE).Count(&articleCount)
	models.Db.Model(new(models.Article)).Select("article_content").Find(&allArticleContent)
	for i := 0; i < len(allArticleContent); i++ {
		wordCount += len(allArticleContent[i])
	}
	models.Db.Model(new(models.Article)).Select("sum(visit_count)").Scan(&visitCount)
	models.Db.Model(new(models.Category)).Count(&categoryCount)
	models.Db.Model(new(models.Comment)).Count(&commentCount)
	websiteInfoVo.WordCount = wordCount
	websiteInfoVo.LastUpdateTime = lastUpdateTime
	websiteInfoVo.ArticleCount = int(articleCount)
	websiteInfoVo.VisitCount = visitCount
	websiteInfoVo.CategoryCount = int(categoryCount)
	websiteInfoVo.CommentCount = int(commentCount)
	response.ResponseOK(c, websiteInfoVo)
	return

}
func UploadAvatar(c *gin.Context) {
	file, _ := c.FormFile("avatar")
	filename := file.Filename
	ext := path.Ext(filename)
	objectName := helper2.GetUUID() + ext
	filePath, err := file.Open()
	if err != nil {
		response.ResponseError(c, HttpCode.FILE_UPLOAD_ERROR)
		return
	}
	uploadFilePath, err := oss.UploadFile(objectName, filePath)
	if err != nil {
		response.ResponseError(c, HttpCode.FILE_UPLOAD_ERROR)
		return
	}
	models.Db.Model(new(models.WebsiteInfo)).Where("id = ?", 1).Update("webmaster_avatar", uploadFilePath)
	response.ResponseOK(c, uploadFilePath)
	return
}

func UploadBackground(c *gin.Context) {
	file, _ := c.FormFile("background")
	filename := file.Filename
	ext := path.Ext(filename)
	objectName := helper2.GetUUID() + ext
	filePath, err := file.Open()
	if err != nil {
		response.ResponseError(c, HttpCode.FILE_UPLOAD_ERROR)
		return
	}
	uploadFilePath, err := oss.UploadFile(objectName, filePath)
	if err != nil {
		response.ResponseError(c, HttpCode.FILE_UPLOAD_ERROR)
		return
	}
	models.Db.Model(new(models.WebsiteInfo)).Where("id = ?", 1).Update("webmaster_profile_background", uploadFilePath)
	response.ResponseOK(c, uploadFilePath)
	return
}

func GetWebsiteInfo4Admin(c *gin.Context) {
	var websiteInfoVo models.WebsiteInfoVo
	models.Db.Model(new(models.WebsiteInfoVo)).First(&websiteInfoVo)
	response.ResponseOK(c, websiteInfoVo)
	return
}

func UpdateStationmasterInfo(c *gin.Context) {
	var stationmasterInfoDto models.StationmasterInfoDto
	if err := c.ShouldBindJSON(&stationmasterInfoDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	if err := models.Db.Model(new(models.WebsiteInfo)).Where("id = ?", 1).Updates(stationmasterInfoDto).Error; err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	response.ResponseOK(c, nil)
	return

}
func UpdateWebsiteInfo(c *gin.Context) {
	var websiteInfoDto models.WebsiteInfoDto
	if err := c.ShouldBindJSON(&websiteInfoDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	if err := models.Db.Model(new(models.WebsiteInfo)).Where("id = ?", 1).Updates(websiteInfoDto).Error; err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	response.ResponseOK(c, nil)
	return
}
